import 'package:app/data/services/store_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'core/utils/helpers/sp.dart';
import 'routes/my_routes.dart';

void main() async {
  // if (WebRTC.platformIsDesktop) {
  //   debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  // } else if (WebRTC.platformIsAndroid) {
  //   WidgetsFlutterBinding.ensureInitialized();
  //   startForegroundService();
  // }

  WidgetsFlutterBinding.ensureInitialized();
  await init(); //初始化

  // SystemUiOverlayStyle systemUiOverlayStyle =
  //     SystemUiOverlayStyle(statusBarColor: Colors.white12);
  // SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  runApp(
    ScreenUtilInit(
        designSize: Size(1080, 1920),
        builder: () => GetMaterialApp(
              theme: SpUtil.instance.controller.theme,
              initialRoute: SpUtil.instance.controller.token == null
                  ? RouteConfig.LOGIN
                  : RouteConfig.MAIN,
              defaultTransition: Transition.fade,
              getPages: RouteConfig.getPages,
              routingCallback: (routing) {},
            )),
  );
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final StorageController controller = Get.put(StorageController());
    return RefreshConfiguration(
        headerBuilder: () => WaterDropHeader(),
        // Configure the default header indicator. If you have the same header indicator for each page, you need to set this
        footerBuilder: () => ClassicFooter(),
        // Configure default bottom indicator
        headerTriggerDistance: 80.0,
        // header trigger refresh trigger distance
        springDescription:
            SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
        // custom spring back animate,the props meaning see the flutter api
        maxOverScrollExtent: 100,
        //The maximum dragging range of the head. Set this property if a rush out of the view area occurs
        maxUnderScrollExtent: 0,
        // Maximum dragging range at the bottom
        enableScrollWhenRefreshCompleted: true,
        //This property is incompatible with PageView and TabBarView. If you need TabBarView to slide left and right, you need to set it to true.
        enableLoadingWhenFailed: true,
        //In the case of load failure, users can still trigger more loads by gesture pull-up.
        hideFooterWhenNotFull: false,
        // Disable pull-up to load more functionality when Viewport is less than one screen
        enableBallisticLoad: true,
        child: GetMaterialApp(
          theme: controller.theme,
          initialRoute:
              controller.token == null ? RouteConfig.LOGIN : RouteConfig.MAIN,
          defaultTransition: Transition.fade,
          getPages: RouteConfig.getPages,
          routingCallback: (routing) {},
        ));
  }
}

// Future<bool> startForegroundService() async {
//   final androidConfig = FlutterBackgroundAndroidConfig(
//     notificationTitle: 'Title of the notification',
//     notificationText: 'Text of the notification',
//     notificationImportance: AndroidNotificationImportance.Default,
//     notificationIcon: AndroidResource(
//         name: 'background_icon',
//         defType: 'drawable'), // Default is ic_launcher from folder mipmap
//   );
//   return FlutterBackground.initialize(androidConfig: androidConfig);
// }

init() async {
  await GetStorage.init();
  // DioManager().init(baseURL: sprintf("%s:%d", [Apis.BASE_URL, Apis.PORT]));
  Get.lazyPut(() => StorageController());
  Get.lazyPut(() => StoreService());
}
